<?php
namespace app\common\component;

use think\facade\Lang;
use think\facade\View;

class ContactInfoComponent extends BaseComponent
{
    /**
     * 组件类型
     * @var string
     */
    protected $type = 'contact_info';
    
    /**
     * 默认配置（每个语言独立完整数据）
     * @var array
     */
    protected $defaultConfig = [
        // 通用样式配置（跨语言共享）
        'section_id' => 'contact-info',
        'layout' => 'grid', // grid 或 list
        'columns' => [
            'mobile' => 1,
            'tablet' => 2,
            'desktop' => 4
        ],
        'card_style' => 'shadow', // shadow 或 flat
        'show_title' => true,
        'data' => [
            // 中文完整数据
            'zh-cn' => [
                'section_title' => '联系我们',
                'section_subtitle' => '选择以下方式与我们取得联系',
                'items' => [
                    'phone' => [
                        'id' => 'phone',
                        'title' => '电话咨询',
                        'content' => '400-123-4567',
                        'icon' => 'fa-phone',
                        'description' => '周一至周五 9:00-18:00',
                        'link' => 'tel:4001234567',
                        'accent_color' => 'blue-500'
                    ],
                    'email' => [
                        'id' => 'email',
                        'title' => '电子邮箱',
                        'content' => 'contact@example.com',
                        'icon' => 'fa-envelope',
                        'description' => '24小时内回复',
                        'link' => 'mailto:contact@example.com',
                        'accent_color' => 'green-500'
                    ],
                    'address' => [
                        'id' => 'address',
                        'title' => '公司地址',
                        'content' => '北京市朝阳区建国路88号现代大厦15层',
                        'icon' => 'fa-map-marker-alt',
                        'description' => '欢迎前来参观洽谈',
                        'link' => '#map',
                        'accent_color' => 'orange-500',
                        'latitude' => '39.9042',
                        'longitude' => '116.4074',
                        'zoom' => 15,
                        'height' => '400px'
                    ],
                    'chat' => [
                        'id' => 'chat',
                        'title' => '在线客服',
                        'content' => '工作日 9:00-18:00',
                        'icon' => 'fa-comments',
                        'description' => '实时在线咨询',
                        'link' => 'javascript:openChat()',
                        'accent_color' => 'purple-500'
                    ]
                ]
            ],
            
            // 英文完整数据
            'en-us' => [
                'section_title' => 'Contact Us',
                'section_subtitle' => 'Choose the following ways to get in touch with us',
                'items' => [
                    'phone' => [
                        'id' => 'phone',
                        'title' => 'Phone Consultation',
                        'content' => '400-123-4567',
                        'icon' => 'fa-phone',
                        'description' => 'Monday to Friday 9:00-18:00',
                        'link' => 'tel:4001234567',
                        'accent_color' => 'blue-500'
                    ],
                    'email' => [
                        'id' => 'email',
                        'title' => 'Email',
                        'content' => 'contact@example.com',
                        'icon' => 'fa-envelope',
                        'description' => 'Reply within 24 hours',
                        'link' => 'mailto:contact@example.com',
                        'accent_color' => 'green-500'
                    ],
                    'address' => [
                        'id' => 'address',
                        'title' => 'Company Address',
                        'content' => '15th Floor, Modern Building, 88 Jianguo Road, Chaoyang District, Beijing',
                        'icon' => 'fa-map-marker-alt',
                        'description' => 'Welcome to visit and discuss',
                        'link' => '#map',
                        'accent_color' => 'orange-500',
                        'latitude' => '39.9042',
                        'longitude' => '116.4074',
                        'zoom' => 15,
                        'height' => '400px'
                    ],
                    'chat' => [
                        'id' => 'chat',
                        'title' => 'Online Customer Service',
                        'content' => 'Working days 9:00-18:00',
                        'icon' => 'fa-comments',
                        'description' => 'Real-time online consultation',
                        'link' => 'javascript:openChat()',
                        'accent_color' => 'purple-500'
                    ]
                ]
            ]
        ]
    ];
    
    /**
     * 渲染组件
     * @return string
     */
    public function render()
    {
        // 合并配置
        $config = array_merge($this->defaultConfig, $this->config);
        
        // 获取当前语言
        $langSet = Lang::getLangSet();
        $fallbackLang = 'zh-cn'; // 默认语言
        
        // 获取当前语言完整数据，不存在则使用默认语言
        $langData = $config['data'][$langSet] ?? $config['data'][$fallbackLang];
        
        // 处理项目样式
        $processedItems = $this->processItemStyles($langData['items']);
        
        // 准备模板数据
        $data = [
            'config' => $config,
            'lang' => [
                'section_title' => $langData['section_title'],
                'section_subtitle' => $langData['section_subtitle']
            ],
            'items' => $processedItems,
            'grid_classes' => $this->getGridClasses($config['columns']),
            'card_base_classes' => $this->getCardBaseClasses($config['card_style'])
        ];
        
        View::assign('data', $data);
        return View::fetch('component/' . $this->type, $data)->getContent();
    }
    
    /**
     * 处理项目样式
     * @param array $items
     * @return array
     */
    protected function processItemStyles(array $items)
    {
        foreach ($items as &$item) {
            $color = $item['accent_color'] ?? 'blue-500';
            
            // 生成样式类
            $item['styles'] = [
                'icon' => "w-12 h-12 rounded-full bg-{$color}/10 text-{$color} flex items-center justify-center mb-4 text-xl",
                'title' => "text-lg font-semibold mb-2 text-gray-900",
                'content' => "text-lg font-medium text-{$color} mb-2",
                'description' => "text-gray-600 text-sm"
            ];
        }
        
        return $items;
    }
    
    /**
     * 获取网格布局类
     * @param array $columns
     * @return string
     */
    protected function getGridClasses(array $columns)
    {
        $mobile = $columns['mobile'] ?? 1;
        $tablet = $columns['tablet'] ?? 2;
        $desktop = $columns['desktop'] ?? 4;
        
        return "grid grid-cols-$mobile md:grid-cols-$tablet lg:grid-cols-$desktop gap-6";
    }
    
    /**
     * 获取卡片基础样式
     * @param string $style
     * @return string
     */
    protected function getCardBaseClasses(string $style)
    {
        $base = "p-6 rounded-xl transition-all duration-300 hover:-translate-y-1";
        
        return $style === 'shadow' 
            ? $base . " bg-white shadow-md hover:shadow-lg" 
            : $base . " bg-white border border-gray-100 hover:border-gray-200";
    }
    
    // AI生成内容
    public function generateByAI($keywords = [])
    {
        return true;
    }
}
    